(function(mui, doc) {
	mui.init({
		statusBarBackground: '#f7f7f7'
	});
	mui.plusReady(function() {
		//设置为强制纵屏幕显示（html5）
		plus.screen.lockOrientation("portrait-primary");
		var mainPage = mui.preload({
			"id": 'main',
			"url": '../../main/main.html'
		});
		var toMain = function() {
			mui.fire(mainPage, 'show', null); //加载数据
			mui.openWindow({
				id : 'main',
				show:{ autoShow:true}
			});
		};
		var settings = app.getSettings(); //本地配置
		var $user = app.getUser(); //当前状态
		if (settings.autoLogin && $user) {
			toMain();
		}
		var loginButton = doc.getElementById('login'); //登录按钮
		var accountBox = doc.getElementById('account'); //账号输入框
		var passwordBox = doc.getElementById('password'); //密码输入框
		var autoLoginButton = doc.getElementById("autoLogin"); //自动登陆开关
		//为登录按钮添加事件
		loginButton.addEventListener('tap', function(event) {
			var loginInfo = {
				account: accountBox.value,
				password: passwordBox.value
			};
			sun.login(loginInfo, function(data) {
				if(data) {
					sun.print("登录成功！");
					app.setUser(data);
					accountBox.blur();
					passwordBox.blur();
					toMain();
				} else {
					sun.print("登录失败,账号密码错误！")
				}
			});
		});
		
		accountBox.addEventListener("keydown",function(e){
	        if(13 == e.keyCode){
	            mui.trigger(loginButton,'tap');
	            accountBox.blur();
	        }
	    },false);
	    
	    passwordBox.addEventListener("keydown",function(e){
	        if(13 == e.keyCode){
	            mui.trigger(loginButton,'tap');
	            passwordBox.blur();
	        }
	    },false);
		
		//初始化自动登陆开关状态
		autoLoginButton.classList[settings.autoLogin ? 'add' : 'remove']('mui-active')
		//为自动登陆开关添加事件
		autoLoginButton.addEventListener('toggle', function(event) {
			setTimeout(function() {
				var isActive = event.detail.isActive;
				settings.autoLogin = isActive;
				app.setSettings(settings);
			}, 50);
		}, false);
		
		//按两次退出应用
		var backButtonPress = 0;
		mui.back = function(event) {
			backButtonPress++;
			if(backButtonPress > 1) {
				plus.runtime.quit(); //执行基座的退出
			} else {
				plus.nativeUI.toast('再按一次退出应用');
			}
			setTimeout(function() {
				backButtonPress = 0;
			}, 1000);
			return false;
		};
	});
}(mui, document));